package com.bj58.data.emailreports.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.bj58.groupbuy.util.PathUtil;
import com.bj58.poster.service.JobReceiveService;
import com.bj58.spat.scf.client.SCFInit;
import com.bj58.spat.scf.client.proxy.builder.ProxyFactory;
/**邮件发送基本工具类
 * @author HL
 *
 */
public class MailUtil {
	/**
	 * 获得使用公共接口发送email服务
	 * 
	 * @return
	 */
	public static JobReceiveService getEmailSendService() {
		String url = "tcp://poster4/PosterService";
		return ProxyFactory.create(JobReceiveService.class, url);
	}
	
	
	public static final String EMAILFROM[] = { "tuangou@58.com.cn",
		"tuangou2@58.com.cn", "tuangou3@58.com.cn", "tuangou4@58.com.cn",
		"tuangou5@58.com.cn", "tuangou6@58.com.cn", "tuangou7@58.com.cn" };
	
	
	@SuppressWarnings("deprecation")
	public static void initSCF() {
		String CONFIG_ROOT_PATH=PathUtil.getWFNamespaceFolder();
		String configPath = CONFIG_ROOT_PATH + "/scf.config";
		System.out.println("configPath:"+configPath);
		System.setProperty("scf.config.path", configPath);
		System.setProperty("scf.jar.path", CONFIG_ROOT_PATH);
		String[] aryPaths = new String[]{CONFIG_ROOT_PATH};
		SCFInit.init(configPath, aryPaths);
	}
	
	
	
	
//	 /**apache 邮件发送
//	 * @param title
//	 * @param content
//	 * @param encode
//	 * @param address
//	 * @return
//	 */
//	public static int apacheSend(String title,String content,String encode,String address)    
//	    {    
//		 	HtmlEmail email = new HtmlEmail();       
//	        email.setTLS(true);            
//		 	email.setHostName("smtp.exmail.qq.com");          
//	        email.setSSL(true);
//	        email.setSslSmtpPort("465");
//	        email.setAuthentication("huangliang@58.com", "19851014hl");       
//	        try{  
//	        	if(address.contains(";")){
//	        		String[] temp  = address.split(";");
//	        		for(String s : temp){
//	        			email.addTo(s);
//	        		}
//	        	}else{
//	        		email.addTo(address);
//	        	}
//	            email.setFrom("huangliang@58.com");       //我方       
//	            email.setSubject(title);                 //标题       
//	            email.setCharset(encode);                     //设置Charset    
//	            email.setHtmlMsg(content);     //内容       
//	            email.send();     
//	            return 1;
//	        } catch (EmailException e) {    
//	            e.printStackTrace();    
//	            return 0;
//	        }     
//	    }    
//	
	
	/**jmail发送邮件
	 * @param title		邮件标题
	 * @param content	正文内容
	 * @param address	收件地址
	 * @param attachedFileList	附件路径
	 * @return
	 */
	public static int javaMailSend(String title,String content,String address,ArrayList<String>  attachedFileList) {
		try {
			MailForJMail mail = new MailForJMail();
			MailSender  sender = new MailSender("smtp.exmail.qq.com", "mail-reporting@58.com", "ecdata58&AB12^sq58");
			mail.setFrom("mail-reporting@58.com");
		    mail.setSubject(title); //标题
	        mail.addBody(content,1);
	        
	        if(null != attachedFileList && !attachedFileList.isEmpty()){
	        	mail.setAttachedFileList(attachedFileList);
	        }
	       
	     	if(address.contains(";")){
	    		String[] temp  = address.split(";");
	    		for(String s : temp){
	    			mail.addTo(s);
	    		}
	    	}else{
	    		mail.addTo(address);
	    	}
	        boolean end = sender.send(mail,1);
	        if(end){
	        	return 1;
	        }else{
	        	return 0;
	        }
		} catch (Exception e) {
		    e.printStackTrace();    
            return 0;
		}
	}
	
	/**调用perl邮件发送模块
	 * 
	 */
	public static void sendEmailByPerl(String title,String content,String address) throws Exception{
		StringBuffer sb = new StringBuffer();
		if(address.contains(";")){
			address = address.replaceAll(";", ",");
		}
		Process proc = Runtime.getRuntime().exec(new String[]{"perl", "/work/opt/analytic/send_mail/send_mail.pl",title,address,content});
		InputStream inputStream = proc.getInputStream();
	    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
	    String line;
	    while((line = bufferedReader.readLine())!=null){
	     sb.append(line).append("\n");
	    }
	    proc.waitFor();
	    System.out.println(sb.toString());
	}
	
}	


